package com.qf.springbootproject.user.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 刘欣
 * @createTime 2023/2/9 19:03
 * @description
 */
@Configuration
public class ShiroConfig {

    @Autowired
    private ShiroProperties shiroProperties;

    /**
     * 配置shiro拦截路径
     *
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition filterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        String[] anonUrls = shiroProperties.getAnonUrls();
        for (String anonUrl : anonUrls) {
            definition.addPathDefinition(anonUrl,"anon");
        }
        definition.addPathDefinition("/**", "authc");
        return definition;
    }

    @Bean
    public Realm realm() {
        CustomRealm customRealm = new CustomRealm();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashIterations(1024);
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return customRealm;
    }
}
